import 'dart:async';

import 'package:casdon/modules/me/application/notification/oss_sts_notifier.dart';
import 'package:casdon/modules/me/application/notification/take_photos_notifier.dart';

import 'package:casdon/auto_router.dart';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class DemoHomePage extends ConsumerStatefulWidget {
  const DemoHomePage({super.key});

  @override
  _DemoHomePageState createState() => _DemoHomePageState();
}

class _DemoHomePageState extends ConsumerState<DemoHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Demo AI 识别')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () => _cameraClickStream(context, ref),
              child: const Text('拍照AI 流 识别包装'),
            ),
            const SizedBox(height: 50),
            ElevatedButton(
              onPressed: () => _openConstomCamera(context),
              child: const Text('拍照AI 识别食材'),
            ),
          ],
        ),
      ),
    );
  }

  // 处理拍照点击事件
  _cameraClickStream(BuildContext context, WidgetRef ref) async {
    final ret = await ref.read(cameraProvider.notifier).takePhoto();
    final url = await ref
        .read(ossTokenProvider.notifier)
        .uploadImageForUrl(ret!);

    if (url == null) return;
    if (context.mounted == false) return;
    DemoSseRoute(url).push(context);
  }

  Future<void> _openConstomCamera(BuildContext context) async {
    DemoCameraRoute().push(context);
  }
}
